package com.xxwy.apply;

import com.xxwy.zk.entity.Lock;
import com.xxwy.zk.lock.ZookeeperLock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author wy
 * @date 2020/4/8 17:58
 */
public class Test {

  /*
  初始化path:
/lock/lock/x0000000001尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000006尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000008尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000004尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000000尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000000 抢到锁了
/lock/x0000000003尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000009尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000002尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000007尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000005尝试获取锁，现在抢锁队列[/lock/x0000000000, /lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000000Thread[pool-1-thread-1,5,main]bye
/lock/x0000000000 锁释放了
/lock/x0000000001尝试获取锁，现在抢锁队列[/lock/x0000000001, /lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000001Thread[pool-1-thread-4,5,main]bye
/lock/x0000000001 锁释放了
/lock/x0000000002尝试获取锁，现在抢锁队列[/lock/x0000000002, /lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000002Thread[pool-1-thread-3,5,main]bye
/lock/x0000000002 锁释放了
/lock/x0000000003尝试获取锁，现在抢锁队列[/lock/x0000000003, /lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000003Thread[pool-1-thread-5,5,main]bye
/lock/x0000000003 锁释放了
/lock/x0000000004尝试获取锁，现在抢锁队列[/lock/x0000000004, /lock/x0000000005, /lock/x0000000006, /lock/x0000000007, /lock/x0000000008, /lock/x0000000009]
/lock/x0000000006没抢到锁+1--------,
/lock/x0000000008没抢到锁+1--------,
/lock/x0000000009没抢到锁+1--------,
/lock/x0000000007没抢到锁+1--------,
/lock/x0000000005没抢到锁+1--------,
/lock/x0000000004Thread[pool-1-thread-6,5,main]bye
   */

  public static void main(String[] args) throws InterruptedException {
    ZookeeperLock zookeeperLock = new ZookeeperLock("/lock");
    ExecutorService service = Executors.newFixedThreadPool(20);
    for (int i = 0; i < 10; i++) {
      service.execute(() -> {
        Lock lock = zookeeperLock.lock("x", 5000);
        if (lock.isActive()) {
          try {
            TimeUnit.SECONDS.sleep(1);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          zookeeperLock.unLock(lock);
        } else {
          System.out.println(lock.getPath() + "没抢到锁+1--------,");
        }
      });
    }
    service.shutdown();
    if (Thread.activeCount() > 2) {
      Thread.sleep(0);
    }
  }
}
